package servlet.Course;

import bean.Course;
import bean.Student;
import dao.impl.CourseDaoImpl;
import dao.impl.StudentIDaoImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/course/updateCourse")
public class CourseXiGaiByID extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        String cno = req.getParameter("cno");
        CourseDaoImpl dao = new CourseDaoImpl();
        Course course = dao.getCourseById(cno);
        HttpSession session = req.getSession();
        System.out.println("修改："+course);
        session.setAttribute("course", course);
        resp.sendRedirect("/course/updateCourse.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        String cno = req.getParameter("cno");
        String cname = req.getParameter("cname");
        String cpno = req.getParameter("cpno");
        String credit = req.getParameter("credit");
        Course course = new Course();
        course.setCno(cno);
        course.setCname(cname);
        course.setCpno(cpno);
        course.setCredit(Double.valueOf(credit));
        CourseDaoImpl dao = new CourseDaoImpl();
        HttpSession session = req.getSession();
        int row = dao.updateCourse(course);
        if (row > 0) {
            req.getRequestDispatcher("/courseAllServlet").forward(req,resp);
        }else{
            session.setAttribute("msg","内部错误，稍后重试！"); //存储到缓存
            resp.sendRedirect("/err/error.jsp");
        }
    }
}
